/*
 * Copyright (c) GBA-NCTI-ISDC. 2022-2024.
 *
 * openGauss embedded is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 * http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITFOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 * -------------------------------------------------------------------------
 *
 * cast_expression.cpp
 *
 * IDENTIFICATION
 * openGauss-embedded/src/compute/sql/planner/expressions/cast_expression.cpp
 *
 * -------------------------------------------------------------------------
 */
#include "planner/expressions/cast_expression.h"

#include "type/operator/decimal_cast.h"
#include "type/value.h"

auto CastExpression::Evaluate(const Record& record) const -> Value {
    auto value = child_->Evaluate(record);
    try {
        return ValueCast::CastValue(value, target_type_);
    } catch (intarkdb::Exception& intarkdb_ex) {
        if (try_cast_) {
            return ValueFactory::ValueNull(target_type_);
        }
        throw intarkdb_ex;
    } catch (const std::runtime_error& ex) {
        if (try_cast_) {
            return ValueFactory::ValueNull(target_type_);
        }
        throw ex;
    }
}
